﻿

#include "zyPictureBox.h"


DarkMoonUI::zyPictureBox::zyPictureBox()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyPictureBox_WndProc);
	m_obj->setDestroy(zyPictureBox_Destroy);
	SetMember(L"ShowImageType", 图片显示方式_平铺);
}
DarkMoonUI::zyPictureBox::~zyPictureBox()
{

}
void WINAPI DarkMoonUI::zyPictureBox::zyPictureBox_Destroy(zyObject* obj)
{
	zyPictureBox* zyb = (zyPictureBox*)obj->getPtrData(L"this");
	zyb->OnNCDestroy();
	zyCtrol::zyCtrol_Destroy(obj);
}
LRESULT WINAPI DarkMoonUI::zyPictureBox::zyPictureBox_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	LRESULT ret = zyCtrol::zyCtrol_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandle);
	if (!bHandle)
		return ret;
	if (uMsg == WM_NCDESTROY) {
		zyPictureBox* zyb = (zyPictureBox*)obj->getPtrData(L"this");
		zyb->OnNCDestroy();
	}
	else if (uMsg == WM_ERASEBKGND) {
		zyPictureBox* p = (zyPictureBox*)obj->getPtrData(L"this");
		if (p->OnDrawBack(hWnd, (HDC)wParam)) {
			*bHandle = true;
			return 1;
		}
	}
	else if (uMsg == zyWm_PlayGif) {
		LONG_PTR hImage = (LONG_PTR)obj->getPtrData(L"pBackImage");
		if (hImage == 0)
			return 0;
		//Gdiplus::GpImage* image = reinterpret_cast<Gdiplus::GpImage*>(hImage);
		GUID pageGuid = FrameDimensionTime;
		//int ret = Gdiplus::DllExports::GdipImageSelectActiveFrame(image ,&pageGuid, (UINT)wParam);
		Gdiplus::Image* image = reinterpret_cast<Gdiplus::Image*>(hImage);
		// 选择当前帧
		image->SelectActiveFrame(&pageGuid, (UINT)wParam);
		if (ret != 0) {
			*bHandle = true;
			return ret;
		}
		InvalidateRect(hWnd, 0, true);
	}
	else if (uMsg == WM_TIMER) {
		zyPictureBox* p = (zyPictureBox*)obj->getPtrData(L"this");
		p->zyPictureBox_OnTimer(hWnd);
	}
	return 0;
}
void DarkMoonUI::zyPictureBox::zyPictureBox_OnTimer(HWND hWnd) {
	int FrameCount = (int)(INT_PTR)GetMember(L"PlayFrameCount");
	int PlayIndex = (int)(INT_PTR)GetMember(L"PlayIndex");
	if (PlayIndex >= FrameCount)
		PlayIndex = 0;
	INT ret = (int)SendMessage(hWnd, zyWm_PlayGif, PlayIndex, 0);
	if (ret != 0) {
		PlayIndex = 0;
		SetMember(L"PlayIndex", PlayIndex);
		return;
	}
	PlayIndex = PlayIndex + 1;
	SetMember(L"PlayIndex", PlayIndex);
}
void DarkMoonUI::zyPictureBox::OnNCDestroy() {
	LONG_PTR pBackImage = GetMember(L"pBackImage");
	if (pBackImage != 0) {
		//IUnknown_Release (pBackImage);
		//if (pBackImage) Gdiplus::DllExports::GdipDisposeImage(reinterpret_cast<Gdiplus::GpImage*>(pBackImage));
		SetMember(L"pBackImage", 0);
	}
	LONG_PTR TimerId = GetMember(L"TimerId");
	if (TimerId != 0) {
		KillTimer(getHwnd(), TimerId);
		SetMember(L"TimerId", 0);
	}
}
BOOL DarkMoonUI::zyPictureBox::创建(HWND 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	return _CreateCtrol(参数_父窗口, 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 | SS_NOTIFY | SS_OWNERDRAW, 参数_扩展风格, L"Static");
}

BOOL DarkMoonUI::zyPictureBox::创建(zyWindow& 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	return _CreateCtrol(参数_父窗口.getHwnd(), 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 | SS_NOTIFY | SS_OWNERDRAW, 参数_扩展风格, L"Static");
}
bool DarkMoonUI::zyPictureBox::绑定(HWND 参数_父窗口, int 参数_资源ID, bool 参数_使用默认字体) {//成功返回真，失败返回假。
	HWND hwnd = GetDlgItem(参数_父窗口, 参数_资源ID);
	if (hwnd != 0) {
		AddStyle(SS_OWNERDRAW);
	}
	if (参数_使用默认字体 && hwnd != 0) {
		SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
	}
	return _BindWindow(hwnd);
}
bool DarkMoonUI::zyPictureBox::绑定窗口句柄(HWND 参数_窗口句柄) {
	if (IsWindow(参数_窗口句柄) != 0) {
		AddStyle(SS_OWNERDRAW);
	}
	return _BindWindow(参数_窗口句柄);
}
int DarkMoonUI::zyPictureBox::图片显示方式(int 参数_图片显示方式) {//"图片显示方式_"开头常量
	int ShowImageType = (int)(INT_PTR)GetMember(L"ShowImageType");
	if (参数_图片显示方式 > -1) {
		SetMember(L"ShowImageType", 参数_图片显示方式);
		重画();
	}
	return (int)ShowImageType;
}
int DarkMoonUI::zyPictureBox::背景色(int 参数_背景色) {//RGB颜色值
	int color = zyARGB2RGB((int)(INT_PTR)GetMember(L"BackColor"), 255);
	if (参数_背景色 == 0)
		return color;
	SetMember(L"BackColor", zyRGB2ARGB(参数_背景色, 255));
	重画();
	return color;
}
bool DarkMoonUI::zyPictureBox::播放动画(bool 参数_播放动画) {
	SetMember(L"PlayGif", 参数_播放动画);
	UINT_PTR TimerID = GetMember(L"TimerId");
	int Delay = (int)GetMember(L"PlayDelay");
	if (Delay == 0) {
		Delay = 88;
	}
	if (TimerID != 0) {
		if (参数_播放动画) {
			SetTimer(取窗口句柄(), TimerID, Delay, 0);
		}
		else {
			KillTimer(取窗口句柄(), TimerID);
		}
	}
	return GetMember(L"PlayGif") != 0;
}
void DarkMoonUI::zyPictureBox::图片(const std::wstring& 路径) {
	//Gdiplus::GpImage* pBackImage = reinterpret_cast<Gdiplus::GpImage*>(GetMember(L"pBackImage"));
	Gdiplus::Image* pBackImage = reinterpret_cast<Gdiplus::Image*>(GetMember(L"pBackImage"));
	if (pBackImage != nullptr) {
		//Gdiplus::DllExports::GdipDisposeImage(pBackImage);
		delete pBackImage;
	}

	//Gdiplus::GpImage* image = nullptr;
	//Gdiplus::Status lastResult = Gdiplus::DllExports::GdipLoadImageFromFile(路径, &image);

	Gdiplus::Image* image = Gdiplus::Image::FromFile(路径.c_str());
	SetMember(L"pBackImage", (LONG_PTR)image);
	InitGif(image);
	//image.置指针 (pBackImage)
	InvalidateRect(getHwnd(), 0, true);
	//return (image)
}
bool DarkMoonUI::zyPictureBox::InitGif_废弃(Gdiplus::GpImage* image)
{
	if (!image) return false;

	UINT count = 0;
	GpStatus status = Gdiplus::DllExports::GdipImageGetFrameDimensionsCount(image, &count);
	if (status != Ok) {
		return false;
	}

	std::vector<GUID> dimensionIDs(count);
	status = Gdiplus::DllExports::GdipImageGetFrameDimensionsList(image, dimensionIDs.data(), count);
	if (status != Ok) {
		return false;
	}

	UINT m_nFrameCount = 0;
	status = Gdiplus::DllExports::GdipImageGetFrameCount(image, &dimensionIDs[0], &m_nFrameCount);
	if (status != Ok || m_nFrameCount < 2) {
		return false; // 不是动画GIF
	}

	UINT nSize = 0;
	status = Gdiplus::DllExports::GdipGetPropertyItemSize(image, PropertyTagFrameDelay, &nSize);
	if (status != Ok) {
		return false;
	}

	std::unique_ptr<PropertyItem> propertyItem(static_cast<PropertyItem*>(malloc(nSize)));
	if (!propertyItem) {
		return false;
	}

	status = Gdiplus::DllExports::GdipGetPropertyItem(image, PropertyTagFrameDelay, nSize, propertyItem.get());
	if (status != Ok) {
		return false;
	}

	int delay = 88; // 默认延迟时间
	if (propertyItem->value && propertyItem->length >= sizeof(int)) {
		int frameDelay = *(reinterpret_cast<int*>(propertyItem->value));
		if (frameDelay > 0) {
			delay = frameDelay * 11;
		}
	}

	SetMember(L"PlayDelay", delay);
	SetMember(L"PlayFrameCount", m_nFrameCount);
	SetMember(L"PlayIndex", 0);

	INT_PTR TimerId = SetTimer(getHwnd(), 1, delay, nullptr);
	SetMember(L"TimerId", TimerId);

	return true; // 是动画GIF
}
void DarkMoonUI::zyPictureBox::图片(Gdiplus::Image* pImage) {
	Gdiplus::Image* pBackImage = reinterpret_cast<Gdiplus::Image*>(GetMember(L"pBackImage"));
	if (pBackImage != nullptr) {
		delete pBackImage;
	}

	SetMember(L"pBackImage", (LONG_PTR)pImage);
	InitGif(pImage);
	//image.置指针 (pBackImage)
	InvalidateRect(getHwnd(), 0, true);
	//return (image)
}
bool DarkMoonUI::zyPictureBox::InitGif(Gdiplus::Image* image)
{
    if (!image) return false;

    UINT count = image->GetFrameDimensionsCount();

    std::vector<GUID> dimensionIDs(count);
    image->GetFrameDimensionsList(dimensionIDs.data(), count);

    UINT m_nFrameCount = image->GetFrameCount(&dimensionIDs[0]);
    if (m_nFrameCount < 2) {
        return false; // 不是动画GIF
    }

    UINT nSize = image->GetPropertyItemSize(PropertyTagFrameDelay);
    
    std::unique_ptr<PropertyItem> propertyItem(static_cast<PropertyItem*>(malloc(nSize)));
    if (!propertyItem) {
        return false;
    }
	image->GetPropertyItem(PropertyTagFrameDelay, nSize, propertyItem.get());
    
    int delay = 88; // 默认延迟时间
    if (propertyItem->value && propertyItem->length >= sizeof(int)) {
        int frameDelay = *(reinterpret_cast<int*>(propertyItem->value));
        if (frameDelay > 0) {
            delay = frameDelay * 11;
        }
    }

    SetMember(L"PlayDelay", delay);
    SetMember(L"PlayFrameCount", m_nFrameCount);
    SetMember(L"PlayIndex", 0);

    INT_PTR TimerId = SetTimer(getHwnd(), 1, delay, nullptr);
    SetMember(L"TimerId", TimerId);

    return true; // 是动画GIF
}